
import util from "../../common/js/util.js";
import parse from 'mini-html-parser2';
var app = getApp();

// 弹出层状态
const Status = {
  OPEN: 'open',
  CLOSE: 'close',
  OPENED: 'opened',
  CLOSED: 'closed'
}
let pageSize = 10;

Component({
  mixins: [],
  data: {
    showLoadMore: true,
    loadMoreText: "加载中...",
    currentPage: 1,
    showCount: pageSize,
    totalResult: pageSize,
    totalNum: 0,
    COMMENT_LIST: [],
    COMMENT_FILE_LIST: [],
    commentFocus: false,//评论框获取焦点
    commentContent: "",
    title:"评论",
    inputHide:false,
    status: Status.CLOSED
  },
  props: {
    // 弹窗方向
    type: 'top',
    // 横向弹出时，指定弹框宽度
    width: '440rpx',
    // 纵向弹出时，指定弹出高度
    height: 'auto',
    // 自定义弹出层样式
    customStyle: {},
    // 是否开启动画
    animation: true,
    // 圆角模式
    round: false,
    // 边距
    padding: '30rpx',
    PID: "",
    TYPE_NAME: ""
  },
  didMount() { },
  didUpdate() { },
  didUnmount() { },
  ygcComment(ref) {
    this.ygc_comment = ref;
  },
  methods: {
    ygcComment(ref) {
      this.ygc_comment = ref;
    },
    open_ygc_comment() {
      this.ygc_comment.toggleMask("show")
    },
    // 打开弹出层
    open(e) {
      if(e.title){
        this.setData({
          title:e.title
        })
      }
      if(e.inputHide){
        this.setData({
          inputHide:e.inputHide
        })
      }
      this.setData({
        status: this.props.animation ? Status.OPEN : Status.OPENED
      });
      this.setData(e);
      this.pubCommentList()
    },
    // 关闭弹出层
    close() {
      // console.
      console.trace()
      this.setData({
        status: this.props.animation ? Status.CLOSE : Status.CLOSED,
        currentPage: 1,
        showCount: 10,
        totalNum: 0,
        COMMENT_LIST: []
      })
    },

    // 点击遮罩层
    maskClickHandler(e) {
      if (e.target.dataset.role === 'mask') {
        this.close()
      }
    },

    // 动画结束
    onAnimationEnd() {
      if (this.data.status === Status.OPEN) {
        this.setData({
          status: Status.OPENED
        });
      } else if (this.data.status === Status.CLOSE) {
        this.setData({
          status: Status.CLOSED
        })
      }
    },
    onSubmitComment(e) {
      //  console.log(e);
      let comment = e.detail.value;
      this.onPubComment(comment);
    },
    onSendComment() {
      let commentContent = this.data.commentContent;
      this.onPubComment(commentContent);
    },
    onPubComment(comment) {
      // let comment = this.data.commentContent;
      if (util.isEmpty(comment)) {
        util.message("请输入评论内容!");
        return false;
      }
      //  comment = `<p>${comment}</p>`;
      let data = {
        PID: this.data.PID,
        COMMENTS: encodeURI(comment),
        TYPE_NAME: this.data.TYPE_NAME
      }
      let nodes = {};
      let that = this;
      dd.showLoading({
        content: '数据提交中...',
      })
      app.$post("/YhERP/articleread/json/save/URLEncode", data).then(res => {
        if (res.is == 1) {
          dd.hideLoading();
          util.messageSuccess("发表成功")
          this.search();
          // let commentList = this.data.COMMENT_LIST;
          // commentList.unshift({
          //   USER_CN_NAME: app.prototype.$SysUserCnName,
          //   ADD_TIME: util.times("ymd_hds"),
          //   COMMENTS: comment,
          //   is_text: true
          // });
          // this.setData({
          //   COMMENT_LIST: commentList,
          //   commentContent: "",
          //   commentFocus: false
          // })
          //that.ygc_comment.clearContent();
          that.setData({
            commentContent: "",
            commentFocus: false
          });
          dd.hideKeyboard();

        };
      });

    },
    search() {
      this.setData({
        totalNum: 0,
        currentPage: 1,
        showCount: 10,
        totalNum: 0,
        COMMENT_LIST: []
      });
      this.pubCommentList();
    },
    pubCommentList() {

      if (this.data.totalNum > this.data.totalResult) {
        // this.loadMoreText = "暂无更多...";
        this.setData({
          loadMoreText: "暂无更多..."
        })
        return false;
      } else {
        this.setData({
          loadMoreText: "点击加载更多..."
        })
      };
      let data = {
        PID: this.data.PID,
        TYPE_NAME: this.data.TYPE_NAME,
        currentPage: this.data.currentPage,
        showCount: this.data.showCount
      };
      let that = this;
      app.$post("/YhERP/articleread/json/ArticleList", data).then(res => {
        if (res.is == 1) {

          // for (let i in res.data.dataList) {
          //   this.$set(this.commentList, this.commentList.length, res.data.dataList[i]);
          // };
          let commentList = this.data.COMMENT_LIST;
          let currentPage = this.data.currentPage;
          let loadMoreText = "点击加载更多...";
          let totalNum = this.data.currentPage * this.data.showCount;

          if (totalNum >= res.data.totalResult) {
            loadMoreText = "暂无更多...";
          } else {
            loadMoreText: "点击加载更多..."
          }
          //top
          let toView = "bottom";
          if (util.isEmpty(res.data.totalResult)) {
            toView = "top";
          }
          // console.log(res.data.dataList);

          this.currentPage++;
          this.setData({
            totalResult: res.data.totalResult,
            totalNum: totalNum,
            COMMENT_LIST: commentList.concat(res.data.dataList),
            currentPage: currentPage,
            loadMoreText: loadMoreText,
            toView: toView
          })

          //this.currentPage++;
        };
      });
    },
    scroll(e) {
      this.setData({
        scrollTop: e.detail.scrollTop
      })
    },
    onScrollToLower() {
      console.log("--下拉:");

    },
    onFocus() {
      this.setData({
        commentFocus: true
        // toView: "bottom"
      })
    },
    onBlur() {
      this.setData({
        commentFocus: false
      })
    },
    bindKeyInput(e) {
      util.debounce(() => {
        let key = e.currentTarget.dataset.key;
        this.setData({
          [`${key}`]: e.detail.value,
        });
        //console.log(e.detail.value);
      }, 300);
    },
  },
});
